#!/usr/env python3
# -*- coding: utf-8 -*-
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from my_figure import MyFigure

class Ui_matplot_demo(object):
    def setupUi(self, matplot_demo):
        matplot_demo.setObjectName("matplot_demo")
        matplot_demo.resize(1133, 721)

        # 网格化布局
        self.gridLayout = QtWidgets.QGridLayout(matplot_demo)
        self.gridLayout.setObjectName("gridLayout")

        self.plt_module = QtWidgets.QWidget(matplot_demo)
        self.plt_module.setMinimumSize(QtCore.QSize(640, 640))
        self.plt_module.setObjectName("plt_module")
        self.gridLayout.addWidget(self.plt_module, 0, 0, 5, 1)

        self.rd_bt_type_linear = QtWidgets.QRadioButton(matplot_demo)
        self.rd_bt_type_linear.setObjectName("rd_bt_type_linear")
        self.gridLayout.addWidget(self.rd_bt_type_linear, 0, 1, 1, 2)
        
        self.rd_bt_type_arc = QtWidgets.QRadioButton(matplot_demo)
        self.rd_bt_type_arc.setObjectName("rd_bt_type_arc")
        self.gridLayout.addWidget(self.rd_bt_type_arc, 0, 3, 1, 2)

        self.txt_end_x = QtWidgets.QLabel(matplot_demo)
        self.txt_end_x.setObjectName("txt_end_x")
        self.gridLayout.addWidget(self.txt_end_x, 1, 1, 1, 1)

        self.txt_end_y = QtWidgets.QLabel(matplot_demo)
        self.txt_end_y.setObjectName("txt_end_y")
        self.gridLayout.addWidget(self.txt_end_y, 1, 3, 1, 1)

        self.spin_box_end_x = QtWidgets.QSpinBox(matplot_demo)
        self.spin_box_end_x.setObjectName("spin_box_end_x")
        self.gridLayout.addWidget(self.spin_box_end_x, 1, 2, 1, 1)

        self.spin_box_end_y = QtWidgets.QSpinBox(matplot_demo)
        self.spin_box_end_y.setObjectName("spin_box_end_y")
        self.gridLayout.addWidget(self.spin_box_end_y, 1, 4, 1, 1)

        self.txt_r = QtWidgets.QLabel(matplot_demo)
        self.txt_r.setObjectName("txt_r")
        self.gridLayout.addWidget(self.txt_r, 2, 1, 1, 2)

        self.spin_box_r = QtWidgets.QSpinBox(matplot_demo)
        self.spin_box_r.setObjectName("spin_box_r")
        self.gridLayout.addWidget(self.spin_box_r, 2, 3, 1, 2)

        self.txt_tips = QtWidgets.QLabel(matplot_demo)
        self.txt_tips.setObjectName("txt_tips")
        self.gridLayout.addWidget(self.txt_tips, 3, 2, 1, 2)

        self.bt_ok = QtWidgets.QPushButton(matplot_demo)
        self.bt_ok.setObjectName("bt_ok")
        self.gridLayout.addWidget(self.bt_ok, 4, 1, 1, 4)

        self.retranslateUi(matplot_demo)
        QtCore.QMetaObject.connectSlotsByName(matplot_demo)

    def retranslateUi(self, matplot_demo):
        _translate = QtCore.QCoreApplication.translate
        matplot_demo.setWindowTitle(_translate("matplot_demo", "Form"))
        self.bt_ok.setText(_translate("matplot_demo", "插补"))
        self.txt_end_x.setText(_translate("matplot_demo", "直线终点横坐标"))
        self.txt_end_y.setText(_translate("matplot_demo", "直线终点纵坐标"))
        self.txt_r.setText(_translate("matplot_demo", "圆弧半径"))
        self.rd_bt_type_linear.setText(_translate("matplot_demo", "直线插补"))
        self.rd_bt_type_arc.setText(_translate("matplot_plot", "圆弧插补"))
        self.txt_tips.setText(_translate("matplot_demo", "请选择插补类型，并输入数据"))

class myWindow(QWidget, Ui_matplot_demo):
    def __init__(self):
        super(myWindow, self).__init__()
        self.setupUi(self)
        self.setWindowTitle("逐点比较法插补 19机械3班何满俊")
        self.setMinimumSize(0, 0)

        # 第五步：定义MyFigure类的一个实例
        self.F = MyFigure(width=10, height=6, dpi=100)
        #self.F.plot_interpolation()

        # 第六步：在GUI的groupBox中创建一个布局，用于添加MyFigure类的实例（即图形）后其他部件。
        # 在容器中添加一个groupbox对象，在groupbox对象中创建布局
        self.groupBox = QGroupBox(self.plt_module)
        self.groupBox.setMinimumSize(QSize(610, 610))
        self.groupBox.setTitle("插补示例图")

        def connect_bind():
            self.bt_ok.clicked.connect(self.open_pic)
        connect_bind()

        self.glo_plt_figure = QGridLayout(self.groupBox)

    def open_pic(self):
        self.F = MyFigure(width=10, height=6, dpi=100)
        if(self.rd_bt_type_linear.isChecked()):
            _translate = QtCore.QCoreApplication.translate
            self.txt_tips.setText(_translate("matplot_demo", "直线插补"))
            xe = self.spin_box_end_x.value()
            ye = self.spin_box_end_y.value()
            self.F.plot_interpolation(xe, ye, "linear")
        elif(self.rd_bt_type_arc.isChecked()):
            _translate = QtCore.QCoreApplication.translate
            self.txt_tips.setText(_translate("matplot_demo", "圆弧插补"))
            r = self.spin_box_r.value()
            self.F.plot_interpolation(0, r, "arc", r, 0)
        else:
            _translate = QtCore.QCoreApplication.translate
            self.txt_tips.setText(_translate("matplot_demo", "未选择插补类型，请选择插补类型"))
            return
        self.glo_plt_figure.addWidget(self.F, 0, 0)
        self.show()
